<template>
  <div class="vip-container">
    <div class="vip-header">
      <h1>VIP会员专区</h1>
      <div class="user-status" v-if="currentUser">
        <span v-if="isVip" class="vip-status">
          👑 尊贵的 {{ currentUser.username }} VIP会员
        </span>
        <span v-else class="normal-status">
          {{ currentUser.username }}
        </span>
      </div>
    </div>

    <!-- VIP权益介绍 -->
    <div class="vip-benefits">
      <h2>VIP会员尊享权益</h2>
      <div class="benefits-grid">
        <div class="benefit-item">
          <div class="benefit-icon">🏷️</div>
          <h3>专属折扣</h3>
          <p>所有商品享受9折优惠，部分商品低至8折</p>
        </div>
        <div class="benefit-item">
          <div class="benefit-icon">🚀</div>
          <h3>发布优先</h3>
          <p>发布的商品优先展示在搜索结果顶部</p>
        </div>
        <div class="benefit-item">
          <div class="benefit-icon">⚡</div>
          <h3>快速交易</h3>
          <p>交易处理速度提升50%，更快完成交易</p>
        </div>
        <div class="benefit-item">
          <div class="benefit-icon">💬</div>
          <h3>专属客服</h3>
          <p>享受7×24小时专属客服支持</p>
        </div>
        <div class="benefit-item">
          <div class="benefit-icon">🛡️</div>
          <h3>交易保障</h3>
          <p>额外的交易保障服务，购物更安心</p>
        </div>
        <div class="benefit-item">
          <div class="benefit-icon">🎁</div>
          <h3>生日礼包</h3>
          <p>生日当月赠送VIP专属礼包</p>
        </div>
      </div>
    </div>

    <!-- VIP特权展示 -->
    <div class="vip-privileges">
      <h2>VIP特权展示</h2>
      <div class="privileges-container">
        <div class="privilege-card">
          <h3>商品发布特权</h3>
          <ul>
            <li>• 每月可发布30个商品（普通用户10个）</li>
            <li>• 可上传更多商品图片（10张 vs 5张）</li>
            <li>• 商品描述无字数限制</li>
            <li>• 支持商品置顶功能</li>
          </ul>
        </div>
        <div class="privilege-card">
          <h3>交易特权</h3>
          <ul>
            <li>• 免交易手续费（普通用户收取2%）</li>
            <li>• 提现快速到账（1小时 vs 24小时）</li>
            <li>• 支持分期付款</li>
            <li>• 专属物流优惠</li>
          </ul>
        </div>
        <div class="privilege-card">
          <h3>专属活动</h3>
          <ul>
            <li>• 优先参与平台限时秒杀</li>
            <li>• VIP专属特价商品区</li>
            <li>• 定期VIP会员日活动</li>
            <li>• 积分加速累积（1.5倍）</li>
          </ul>
        </div>
      </div>
    </div>

    <!-- VIP升级区域 -->
    <div v-if="!isVip" class="vip-upgrade-section">
      <h2>立即升级VIP会员</h2>
      <div class="vip-plans">
        <div class="plan-card">
          <h3>月度会员</h3>
          <div class="plan-price">
            <span class="price">¥19.9</span>
            <span class="period">/月</span>
          </div>
          <ul class="plan-features">
            <li>• 所有VIP基础权益</li>
            <li>• 会员专属标识</li>
            <li>• 优先客服响应</li>
          </ul>
          <button @click="upgradeVip('monthly')" class="upgrade-button">立即开通</button>
        </div>
        <div class="plan-card popular">
          <div class="popular-badge">最受欢迎</div>
          <h3>年度会员</h3>
          <div class="plan-price">
            <span class="price">¥199</span>
            <span class="period">/年</span>
            <span class="save">省¥39.8</span>
          </div>
          <ul class="plan-features">
            <li>• 所有VIP基础权益</li>
            <li>• 会员专属标识</li>
            <li>• 优先客服响应</li>
            <li>• 赠送3个月会员</li>
            <li>• 生日礼包价值¥50</li>
          </ul>
          <button @click="upgradeVip('yearly')" class="upgrade-button popular-button">立即开通</button>
        </div>
        <div class="plan-card">
          <h3>永久会员</h3>
          <div class="plan-price">
            <span class="price">¥499</span>
            <span class="period">/永久</span>
          </div>
          <ul class="plan-features">
            <li>• 终身享受所有VIP权益</li>
            <li>• 专属永久VIP标识</li>
            <li>• 专属客户经理</li>
            <li>• 终身免交易手续费</li>
            <li>• 生日礼包价值¥100</li>
          </ul>
          <button @click="upgradeVip('lifetime')" class="upgrade-button">立即开通</button>
        </div>
      </div>
    </div>

    <!-- VIP会员中心 -->
    <div v-if="isVip" class="vip-center">
      <h2>VIP会员中心</h2>
      <div class="vip-info">
        <div class="vip-detail">
          <h3>会员信息</h3>
          <p><strong>会员等级：</strong>{{ vipLevel }}</p>
          <p><strong>开通时间：</strong>{{ formatDate(vipStartDate) }}</p>
          <p><strong>到期时间：</strong>{{ formatDate(vipEndDate) }}</p>
          <p><strong>会员积分：</strong>{{ vipPoints }}</p>
        </div>
        <div class="vip-actions">
          <button @click="renewVip" class="action-button">续费会员</button>
          <button @click="viewExclusiveItems" class="action-button">查看专属商品</button>
          <button @click="contactVipService" class="action-button">联系专属客服</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const currentUser = ref(null)
const isVip = ref(false)
const vipLevel = ref('')
const vipStartDate = ref('')
const vipEndDate = ref('')
const vipPoints = ref(0)

// 检查用户登录状态
const checkLoginStatus = () => {
  const userData = localStorage.getItem('currentUser')
  if (userData) {
    currentUser.value = JSON.parse(userData)
    isVip.value = currentUser.value.isVip || false
    if (isVip.value) {
      // 获取VIP详细信息
      vipLevel.value = currentUser.value.vipLevel || '普通VIP'
      vipStartDate.value = currentUser.value.vipStartDate || new Date().toISOString()
      vipEndDate.value = currentUser.value.vipEndDate || new Date().toISOString()
      vipPoints.value = currentUser.value.vipPoints || 0
    }
  } else {
    router.push('/login')
  }
}

// 格式化日期
const formatDate = (dateString) => {
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit'
  })
}

// 升级VIP
const upgradeVip = (plan) => {
  if (!currentUser.value) return
  
  let duration = 0
  let level = '普通VIP'
  
  switch (plan) {
    case 'monthly':
      duration = 30 * 24 * 60 * 60 * 1000 // 30天
      break
    case 'yearly':
      duration = 365 * 24 * 60 * 60 * 1000 // 365天
      level = '高级VIP'
      break
    case 'lifetime':
      duration = 100 * 365 * 24 * 60 * 60 * 1000 // 100年，视为永久
      level = '永久VIP'
      break
  }
  
  const now = new Date()
  const startDate = now.toISOString()
  const endDate = new Date(now.getTime() + duration).toISOString()
  
  // 更新当前用户信息
  currentUser.value.isVip = true
  currentUser.value.vipLevel = level
  currentUser.value.vipStartDate = startDate
  currentUser.value.vipEndDate = endDate
  currentUser.value.vipPoints = 0
  
  localStorage.setItem('currentUser', JSON.stringify(currentUser.value))
  
  // 更新用户列表中的信息
  const users = JSON.parse(localStorage.getItem('users') || '[]')
  const userIndex = users.findIndex(u => u.id === currentUser.value.id)
  if (userIndex !== -1) {
    users[userIndex] = { ...currentUser.value }
    localStorage.setItem('users', JSON.stringify(users))
  }
  
  // 更新状态
  isVip.value = true
  vipLevel.value = level
  vipStartDate.value = startDate
  vipEndDate.value = endDate
  vipPoints.value = 0
  
  alert('恭喜您成功升级为VIP会员！')
}

// 续费VIP
const renewVip = () => {
  alert('续费功能即将上线，敬请期待！')
}

// 查看专属商品
const viewExclusiveItems = () => {
  // 在实际项目中，这里会跳转到专属商品页面或过滤显示专属商品
  alert('专属商品区域即将上线，敬请期待！')
}

// 联系专属客服
const contactVipService = () => {
  alert('正在为您连接专属客服，请稍候...\n客服电话：400-123-4567')
}

// 组件挂载时检查登录状态
onMounted(() => {
  checkLoginStatus()
})
</script>

<style scoped>
.vip-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.vip-header {
  text-align: center;
  margin-bottom: 40px;
  padding: 30px 0;
  background: linear-gradient(135deg, #ffd700, #ff9500);
  border-radius: 12px;
  color: white;
}

.vip-header h1 {
  margin: 0 0 10px 0;
  font-size: 36px;
  font-weight: 700;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
}

.user-status {
  font-size: 18px;
  font-weight: 500;
}

.vip-status {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  animation: glow 2s ease-in-out infinite alternate;
}

@keyframes glow {
  from {
    text-shadow: 0 0 5px rgba(255, 255, 255, 0.7);
  }
  to {
    text-shadow: 0 0 20px rgba(255, 255, 255, 0.9), 0 0 30px rgba(255, 215, 0, 0.6);
  }
}

.vip-benefits {
  margin-bottom: 50px;
}

.vip-benefits h2 {
  text-align: center;
  margin-bottom: 30px;
  font-size: 28px;
  color: #333;
}

.benefits-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
}

.benefit-item {
  background: white;
  padding: 25px;
  border-radius: 12px;
  text-align: center;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
}

.benefit-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.12);
}

.benefit-icon {
  font-size: 48px;
  margin-bottom: 15px;
}

.benefit-item h3 {
  margin-bottom: 10px;
  font-size: 20px;
  color: #333;
}

.benefit-item p {
  color: #666;
  font-size: 15px;
}

.vip-privileges {
  margin-bottom: 50px;
}

.vip-privileges h2 {
  text-align: center;
  margin-bottom: 30px;
  font-size: 28px;
  color: #333;
}

.privileges-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 30px;
}

.privilege-card {
  background: white;
  padding: 30px;
  border-radius: 12px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
  border-left: 4px solid #ffd700;
}

.privilege-card h3 {
  margin-bottom: 15px;
  font-size: 20px;
  color: #333;
}

.privilege-card ul {
  list-style: none;
  padding: 0;
}

.privilege-card li {
  padding: 8px 0;
  color: #666;
  font-size: 15px;
  border-bottom: 1px solid #f0f0f0;
}

.privilege-card li:last-child {
  border-bottom: none;
}

.vip-upgrade-section {
  margin-bottom: 50px;
}

.vip-upgrade-section h2 {
  text-align: center;
  margin-bottom: 30px;
  font-size: 28px;
  color: #333;
}

.vip-plans {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 30px;
}

.plan-card {
  background: white;
  padding: 30px;
  border-radius: 12px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.plan-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.plan-card.popular {
  border: 2px solid #ffd700;
  transform: scale(1.02);
}

.popular-badge {
  position: absolute;
  top: 20px;
  right: -30px;
  background: #ffd700;
  color: white;
  padding: 5px 35px;
  transform: rotate(45deg);
  font-weight: bold;
  font-size: 12px;
}

.plan-card h3 {
  text-align: center;
  margin-bottom: 20px;
  font-size: 22px;
  color: #333;
}

.plan-price {
  text-align: center;
  margin-bottom: 20px;
}

.plan-price .price {
  font-size: 36px;
  font-weight: bold;
  color: #ff9500;
}

.plan-price .period {
  font-size: 18px;
  color: #666;
  margin-left: 5px;
}

.plan-price .save {
  display: block;
  font-size: 14px;
  color: #4caf50;
  margin-top: 5px;
}

.plan-features {
  list-style: none;
  padding: 0;
  margin-bottom: 25px;
}

.plan-features li {
  padding: 10px 0;
  color: #666;
  font-size: 15px;
  border-bottom: 1px solid #f0f0f0;
  text-align: center;
}

.plan-features li:last-child {
  border-bottom: none;
}

.upgrade-button {
  display: block;
  width: 100%;
  padding: 15px;
  background: linear-gradient(135deg, #ffd700, #ff9500);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease;
}

.upgrade-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(255, 215, 0, 0.4);
  filter: brightness(1.05);
}

.upgrade-button.popular-button {
  background: linear-gradient(135deg, #ff9500, #ff5722);
}

.vip-center {
  background: white;
  padding: 30px;
  border-radius: 12px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
}

.vip-center h2 {
  text-align: center;
  margin-bottom: 30px;
  font-size: 28px;
  color: #333;
}

.vip-info {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 30px;
}

.vip-detail h3 {
  margin-bottom: 20px;
  font-size: 20px;
  color: #333;
}

.vip-detail p {
  padding: 10px 0;
  color: #666;
  font-size: 16px;
  border-bottom: 1px solid #f0f0f0;
}

.vip-actions {
  display: flex;
  flex-direction: column;
  gap: 15px;
  justify-content: center;
}

.action-button {
  padding: 15px;
  background: linear-gradient(135deg, #ffd700, #ff9500);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease;
}

.action-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(255, 215, 0, 0.4);
  filter: brightness(1.05);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .vip-container {
    padding: 10px;
  }
  
  .vip-header h1 {
    font-size: 28px;
  }
  
  .benefits-grid,
  .privileges-container,
  .vip-plans,
  .vip-info {
    grid-template-columns: 1fr;
  }
  
  .plan-card {
    padding: 20px;
  }
  
  .vip-info {
    gap: 20px;
  }
}
</style>